<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/8
 * Time: 8:58
 */

namespace app\home\controller;

use app\home\model\Level;
use app\home\model\Package as PackageModel;
use app\home\model\PackageUseLog;
use app\home\model\UserPackage;
use app\Request;

class Package
{
    /**
     * 套餐列表
     */
    public function packageList(Request $request)
    {
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $orderField['id'] = 'desc';
        if (isset($param['price_sort']) && !empty($param['price_sort'])) {
            $orderField['price'] = $param['price_sort'];
            unset($orderField['id']);
        }
        if (isset($param['sales_volume_sort']) && !empty($param['sales_volume_sort'])) {
            $orderField['sales_volume'] = $param['sales_volume_sort'];
            unset($orderField['id']);
        }
        $where[] = ['status', '=', 1];
        if (isset($param['name']) && !empty($param['sales_volume_sort'])) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }
        $userPackageModel = new  UserPackage();
        $userPackage = $userPackageModel->where(['uid' => $request->userInfo['id']])->where('num','>',0)->select()->toArray();
        $packageModel = new PackageModel();
        $packageList = $packageModel->where($where)->order($orderField)->select()->toArray();
        if ($packageList) {
            foreach ($packageList as $key => $value) {
                foreach ($userPackage as $userPackageValue) {
                    // 此优惠券已经领取
                    if ($userPackageValue['package_id'] == $value['id'] ) {
                        unset($packageList[$key]);
                    }
                }
            }
        }
        $count = count($packageList);
        if ($packageList) {
            $packageList = array_splice($packageList, $start, $limit);
        }
        return success('套餐列表', ['list' => $packageList, 'count' => $count]);
    }

    /*
     * 我的套餐
     */
    public function myPackage(Request $request)
    {
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $userPackageModel = new  UserPackage();
        if (isset($param['type']) && $param['type'] == 1) {
            $where[] = ['up.num', '>', 0];
        }
        $orderField['up.id'] = 'desc';
        if (isset($param['price_sort']) && !empty($param['price_sort'])) {
            $orderField['price'] = $param['price_sort'];
            unset($orderField['up.id']);
        }
        if (isset($param['sales_volume_sort']) && !empty($param['sales_volume_sort'])) {
            $orderField['sales_volume'] = $param['sales_volume_sort'];
        }
        if (isset($param['name']) && !empty($param['sales_volume_sort'])) {
            $where[] = ['p.name', 'like', '%' . $param['name'] . '%'];
        }
        $where[] = ['up.uid', '=', $request->userInfo['id']];
        $userPackage = $userPackageModel->alias('up')
            ->leftJoin('package p', 'up.package_id = p.id')
            ->field('up.*,p.name,p.desc,p.sales_volume,p.style,p.price,p.logo')
            ->where($where)
            ->limit($start, $limit)->order($orderField)
            ->select()->toArray();

        $count = $userPackageModel->alias('up')
            ->leftJoin('package p', 'up.package_id = p.id')->where($where)
            ->count();

        return success('我的套餐卡', ['list' => $userPackage, 'count' => $count]);
    }

    /***
     * 未购买套餐的 某套餐详情
     */
    public function packageInfo(Request $request)
    {
        $param = $request->param();
        $packageModel = new  PackageModel();
        $packageInfo = $packageModel->where(['id' => $param['id']])->find();
        return success('套餐详情', $packageInfo);
    }

    /**
     * 我的套餐卡详情
     */
    public function myPackageInfo(Request $request)
    {
        $param = $request->param();
        $userPackageModel = new  UserPackage();
        $myPackageInfo = $userPackageModel->where(['id' => $param['id']])->find();
        if ($myPackageInfo) {
            $packageModel = new  PackageModel();
            $packageInfo = $packageModel->where(['id' => $myPackageInfo['package_id']])->find();
            $myPackageInfo['name'] = $packageInfo['name'];
            $myPackageInfo['desc'] = $packageInfo['desc'];
            $myPackageInfo['price'] = $packageInfo['price'];
        }
        return success('我的套餐详情', $myPackageInfo);
    }


    public function packageParameter(Request $request)
    {
        $param = $request->param();
        $userPackageModel = new  UserPackage();
        $userPackage = $userPackageModel->where(['id' => $param['id'], 'uid' => $request->userInfo['id']])->find();
        if (empty($userPackage)) {
            return error('此套餐卡不存在');
        }
        if ($userPackage['num'] <= 0) {
            return error('此套餐卡已使用完');
        }
        $data = [
            'rand_code' => createNonceStr(6)
        ];
        $userPackage['rand_code'] = $data['rand_code'];
        $userPackageModel->where(['id' => $param['id'], 'uid' => $request->userInfo['id']])->update($data);
        return success('套餐卡核销码参数', $userPackage);
    }


    public function usePackageLog(Request $request)
    {
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where['uid'] = $request->userInfo['id'];
        if (isset($param['id']) && !empty($param['id'])){
            $where['user_package_id'] = $param['id'];
        }
        $userPackageLogModel = new  PackageUseLog();
        $data = $userPackageLogModel
            ->where($where)->limit($start, $limit)->select()->toArray();
        $count = $userPackageLogModel
            ->where($where)->count();
        if ($data){
            $packageId = array_column($data,'package_id');
            $PackageModel  = new \app\home\model\Package();
            $packageList = $PackageModel->where('id','in',$packageId)->select()->toArray();
            $packageListById = array_column($packageList,null,'id');
            foreach ($data as $key => $value){
                if (array_key_exists($value['package_id'],$packageListById)){
                    $data[$key]['name'] = $packageListById[$value['package_id']]['name'];
                    $data[$key]['desc'] = $packageListById[$value['package_id']]['desc'];
                    $data[$key]['style'] = $packageListById[$value['package_id']]['style'];
                }
            }
        }
        return success('核销记录', ['list' => $data, 'count' => $count]);
    }

    public function discount(Request $request)
    {
        $param = $request->param();
        $discount = 0;
        $levelModel = new Level();
        $levelInfo = $levelModel->where(['id' => $request->userInfo['level_id']])->find();
        if ($levelInfo['package_preferential'] > 0) { // 针对商城全局的折扣
            $discount  = $levelInfo['package_preferential'];
        } else {
            if ($levelInfo['preferential'] > 0) {  // 全局的折扣
                $discount  = $levelInfo['preferential'];
            }
        }
        return success('', [ 'discount' => $discount]);
    }
}
